package com.lingchen.boot.utils;

import com.alibaba.fastjson.JSON;
import com.lingchen.boot.utils.qq.Base64;

import java.util.Map;

/**
 * Created by lc on 2019/5/21
 */
public class MmmhUtil {

    public static byte[] mapToStr(Map<String, Object> data) {
        String map = JSON.toJSONString(data);

        long time = (System.currentTimeMillis() / 1000);
        long j = 1000000000;
        if (time >= 1000000000) {
            j = time;
        }

        StringBuilder stringBuilder = new StringBuilder(map);
        stringBuilder.append(j);
        byte[] b = stringBuilder.toString().getBytes();
        char[] cArr = new char[b.length];
        for (int i = 0; i < b.length; i++) {
            cArr[i] = (char) (b[i] & 255);
        }
        int[] encr = new int[cArr.length];
        int i2 = 0;
        int i3 = 1;
        for (int i4 = 0; i4 < cArr.length; i4++) {
            int i5 = i4 % 5;
            if (i5 == 0) {
                i3 = i2 % 2 == 0 ? 1 : -1;
                i2++;
            }
            if (cArr[i4] >= 128) {
                encr[i4] = cArr[i4] - 125;
            } else {
                encr[i4] = (cArr[i4] + 110) + ((i5 + 1) * i3);
            }
        }
        return convertToBase64(encr);
    }

    public static byte[] convertToBase64(int[] iArr) {
        byte[] bArr = new byte[iArr.length];
        for (int i = 0; i < iArr.length; i++) {
            bArr[i] = (byte) iArr[i];
        }
        return Base64.encode(bArr, 2);
    }
}
